package com.tb.controller;

import com.tb.model.AComment;
import com.tb.model.Message;
import com.tb.model.QComment;
import com.tb.service.ACommentService;
import com.tb.service.AnswerService;
import com.tb.service.MessageService;
import com.tb.service.QCommentService;
import com.tb.util.MyConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @description: 评论相关，包括问题于答案的评论
 * @author: wangguichun
 * @create: 2018-04-15 00:43
 **/

@Controller
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private QCommentService qCommentService;
    @Autowired
    private ACommentService aCommentService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private AnswerService answerService;

    @RequestMapping("question")
    @ResponseBody
    public void commentQuestion(QComment qComment, int questionUserId) {
        qCommentService.addComment(qComment);
        Message message = new Message();
        message.setFromId(qComment.getUserId());
        message.setToId(questionUserId);
        message.setType(MyConstant.ACT_COMMENT_QUES);
        message.setObjecId(qComment.getQuestionId());
        message.setTime(new Date().getTime());
        messageService.addMessage(message);
    }

    @RequestMapping("answer")
    @ResponseBody
    public void commentAnswer(AComment aComment, int questionId) {
        aCommentService.addComment(aComment);
        Message message = new Message();
        message.setFromId(aComment.getUserId());
        int toId = answerService.getAnswerByAnswerId(aComment.getAnswerId()).getUserId();
        message.setToId(toId);
        message.setType(MyConstant.ACT_COMMENT_ANS);
        message.setObjecId(questionId);
        message.setTime(new Date().getTime());
        messageService.addMessage(message);
    }


}
